package com.huayue.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.huayue.annotation.LogAnno;
import com.huayue.entity.AuthorityList;
import com.huayue.entity.EleTree;
import com.huayue.entity.MenuOut;
import com.huayue.entity.Role;
import com.huayue.service.RoleService;
import com.huayue.util.JSONHelper;
import com.huayue.util.MapToBean;
import com.huayue.util.PageHelper;

/**
 * 角色
 * 
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService svc;

    BaseController bc = new BaseController();

    @RequestMapping("/toAdd")
    public ModelAndView toAdd() {
        FastJsonJsonView view = new FastJsonJsonView();

        view.addStaticAttribute("status", 200);
        view.addStaticAttribute("data", svc.findAllTopRole());
        view.addStaticAttribute("depdata", svc.getDepartmentDao().findAll());
        return new ModelAndView(view);
    }

    @LogAnno(operateType = "添加-角色")
    @RequestMapping("/add")
    public ModelAndView add(AuthorityList autList) {
        // 解析json
        JSONArray authorityJsonArray = JSONHelper.StringArrayToJsonArray(autList.getAuthoritys());
        List<EleTree> eleTrees = JSONHelper.JsonArrayToEleTree(authorityJsonArray);
        FastJsonJsonView view = new FastJsonJsonView();
        svc.authorityInsertBatch(eleTrees);
        return bc.add(svc,
            new Role(autList.getParentid(), autList.getName(), autList.getRemark(), autList.getDep_id()));
    }

    @LogAnno(operateType = "修改-角色")
    @RequestMapping("/update")
    public ModelAndView update(AuthorityList autList) {
        // 解析json
        JSONArray authorityJsonArray = JSONHelper.StringArrayToJsonArray(autList.getAuthoritys());
        List<EleTree> eleTrees = JSONHelper.JsonArrayToEleTree(authorityJsonArray);
        FastJsonJsonView view = new FastJsonJsonView();
        svc.authorityInsertBatch(eleTrees, new Role(autList.getId()));
        return bc.update(svc, new Role(autList.getId(), autList.getParentid(), autList.getName(), autList.getRemark(),
            autList.getDep_id()));
    }

    @LogAnno(operateType = "删除-角色")
    @RequestMapping("/delete")
    public ModelAndView delete(Role role) {
        return bc.delete(svc, role);
    }

    @RequestMapping(value = "/findOne", produces = {"text/javascript;charset=UTF-8"})
    public ModelAndView findOne(Role role) throws Exception {
        ModelAndView modelAndView = bc.findOne(svc, role);
        modelAndView.addObject("menudata", svc.menuGoTreeByRoleId(role));
        return modelAndView;
    }

    @RequestMapping("/findPage")
    public ModelAndView findPage(PageHelper page) {
        return bc.findPage(svc, page, null);
    }

    /**
     * 导出树状图 前台配合eleTree使用
     */
    @RequestMapping("/menuEleTree")
    public ModelAndView menuEleTree() throws Exception {
        FastJsonJsonView view = new FastJsonJsonView();
        JSONArray jsonArray = new JSONArray(MapToBean.convertListMap2ListBean(svc.menuGoTree(), MenuOut.class));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", 0);
        view.addStaticAttribute("code", 0);
        view.addStaticAttribute("data", JSONHelper.getJsontree(jsonArray, jsonObject).getJSONArray("children"));
        return new ModelAndView(view);
    }

}
